package com.shitu.soulemo.a;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.shitu.base.BaseFragment;
import com.shitu.base.net.API;
import com.shitu.base.net.model.Banners;
import com.shitu.base.net.model.Tabs;
import com.shitu.base.net.model.result.PRecommend;
import com.shitu.base.net.model.result.PRecommendChange;
import com.shitu.base.net.service.ProviderService;
import com.shitu.base.net.service.RequireService;
import com.shitu.soulemo.R;
import com.shitu.soulemo.utils.AccountHelper;
import com.shitu.soulemo.utils.GlideImageLoader;
import com.youth.banner.Banner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by majie on 2018/3/20.
 */

public class TuijianFragment extends BaseFragment {
    private View header;
    private GridView gridView;
    private XRecyclerView list;
    private Banner banner;
    private TextView change;
    private TjGridAdpater gridAdpater;
    private List<Tabs> interest;
    private List<String> images = new ArrayList<>();
    private List<Tabs> nearby;
    private List<Tabs> high;
    private ProviderAdapter pAdapter;
    private RequireAdapter rAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tuijian_fragment, null);
        banner = view.findViewById(R.id.banner);
        list = view.findViewById(R.id.list);
        header = inflater.inflate(R.layout.tuijian_header, null);
        gridView = header.findViewById(R.id.grid);
        change = header.findViewById(R.id.change);

        initViews();
        return view;
    }

    private void initViews() {
        gridAdpater = new TjGridAdpater(this);
        gridView.setAdapter(gridAdpater);
        pAdapter = new ProviderAdapter(this);
        rAdapter = new RequireAdapter(this);

        banner.setImageLoader(new GlideImageLoader());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        list.setLayoutManager(layoutManager);
        list.setPullRefreshEnabled(false);
        list.addHeaderView(header);
        if (AccountHelper.isRequire()) {
            list.setAdapter(rAdapter);
            getRdata();
            change.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    refreshRData();
                }
            });

        } else {
            list.setAdapter(pAdapter);
            getPdata();
            change.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    refreshPData();
                }
            });
        }
    }

    private void getPdata() {
        API.send(ProviderService.getHomePageRecommend(), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("tag", "fail");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                PRecommend recommend = JSON.parseObject(str, PRecommend.class);
                List<Banners> list = recommend.getData().getBanners();
                for (int i = 0; i < list.size(); i++) {
                    images.add(list.get(i).getImgUrl());
                }
                interest = recommend.getData().getInterest();
                nearby = recommend.getData().getNearby();
                high = recommend.getData().getHighQualityRecommend();
                if (getActivity() != null) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            refreshUi();
                        }
                    });
                }
            }
        });
    }

    private void getRdata() {
        API.send(RequireService.getHomePageRecommend(), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("tag", "fail");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                PRecommend recommend = JSON.parseObject(str, PRecommend.class);
                List<Banners> list = recommend.getData().getBanners();
                for (int i = 0; i < list.size(); i++) {
                    images.add(list.get(i).getImgUrl());
                }
                interest = recommend.getData().getInterest();
                nearby = recommend.getData().getNearby();
                high = recommend.getData().getHighQualityRecommend();
                if (getActivity() != null) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            refreshUi();
                        }
                    });
                }
            }
        });
    }

    private void refreshPData() {
        API.send(RequireService.getHomePageRecommendChange("1,2,3,4,5,6"), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("tag", "fail change");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                PRecommendChange data = JSON.parseObject(str, PRecommendChange.class);
                interest = data.getData();
                if (getActivity() != null) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            refreshInterest();
                        }
                    });
                }
            }
        });
    }

    private void refreshRData() {
        API.send(ProviderService.getHomePageRecommendChange("1,2,3,4,5,6"), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("tag", "fail change");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                PRecommendChange data = JSON.parseObject(str, PRecommendChange.class);
                interest = data.getData();
                if (getActivity() != null) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            refreshInterest();
                        }
                    });
                }
            }
        });
    }


    private void refreshUi() {
        banner.setImages(images);
        banner.start();

        gridAdpater.setData(interest);
        gridAdpater.notifyDataSetChanged();

        if (AccountHelper.isRequire()) {
            rAdapter.setData(nearby, high);
            rAdapter.notifyDataSetChanged();
        } else {
            pAdapter.setData(nearby, high);
            pAdapter.notifyDataSetChanged();
        }

    }

    private void refreshInterest() {
        gridAdpater.setData(interest);
        gridAdpater.notifyDataSetChanged();
    }
}
